import WatchValue, { SaveEmit } from '../WatchValue'

/** 数值监视类 */
export default class WatchNumber extends WatchValue<number>{

	/**
	 * 绑定监视列表值总和
	 * @param list 被监视的列表
	 */
	public BindSum(...list: WatchValue<number>[]): void {
		const calc = () => list.reduce((x, y) => x + y.Value, 0);
		this.Value = calc();
		list.forEach((item) => item.Add(this, calc));
	}

	/**
	 * 绑定监视列表值差值
	 * @param list 被监视的列表
	 */
	public BindSub(...list: WatchValue<number>[]): void {
		const calc = () => list.slice(1).reduce((x, y) => x - y.Value, list[0].Value);
		this.Value = calc();
		list.forEach((item) => item.Add(this, calc));
	}

	/**
	 * 绑定监视列表值总和
	 * @param list 被监视的列表
	 * @retuern 创建的监视项
	 */
	public static CreatSum(...list: WatchValue<number>[]): WatchNumber {
		const ret = new WatchNumber(0);
		ret.BindSum(...list);
		return ret;
	}

	/**
	 * 绑定监视列表值差值
	 * @param list 被监视的列表
	 * @retuern 创建的监视项
	 */
	public static CreatSub(...list: WatchValue<number>[]): WatchNumber {
		const ret = new WatchNumber(0);
		ret.BindSub(...list);
		return ret;
	}

}
